require "Common/define"
require "Logic/Config"
require "Common/functions"

VipManager = {};
local this = VipManager;
local vipTableData;

function VipManager.init()
    vipTableData = G.dataTable["t_vip"];
end

function VipManager.OnDestroy()
    if this.privilegeTime ~= nil then
        utimer.remove(this.privilegeTime);
    end
end

--- 接受后端VIP信息
function VipManager.resVipInfoMsg(msg)
    this.vipLevel = tonumber(tostring(msg.vipLv));
    --判断贵族等级是否提升
    if this.lastVipLevel and this.lastVipLevel < tonumber(tostring(msg.vipLv)) then
        logicMgr.GuideMgr.Pause()
        showUI(PanelNames.UICommonLevelUp, this.lastVipLevel, this.vipLevel,
                function ()
                    logicMgr.GuideMgr.Resume()
                    if not logicMgr.GuideMgr.IsGuiding() then
                        logicMgr.FlashGiftManager.showNewFlashGift();
                    end
                end,EnumConst.LevelUpType.Vip
        );
        -- 解锁功能，发起协议
        logicMgr.UnlockFuncManager.updateUnlockFunc();
    end
    this.lastVipLevel = this.vipLevel

    TAManager.UserSet("vip_level",this.vipLevel)
    
    this.vipExp = tonumber(tostring(msg.vipExp));
    this.vipFetchList = msg.fetchLvList;
    logicMgr.CostManager.setPlayerBehaviorTime(Config.CostOpenCondition.AchieveVip, this.vipLevel)
    FireEvent(Config.EventType.VIPInfoUpdate);
    FireEvent(Config.EventType.UICostUpdate);
    logicMgr.CostManager.updateNotOpenActive();
    logicMgr.EcoBuffManager.updateEcoBuff(EnumConst.EcoBuffType.VIP, this.getVipEcoBuff())

    TAManager.UserSet("vip_experience", this.vipExp)
end

--获取VIP奖励
function VipManager.getVipDrawReward(id, callBack, tb)
    this.getRewardId = id;
    this.getRewardCallBack = callBack;
    this.getRewardtb = tb;
    vipHandler.reqGetVipDrawReward(tonumber(id));
end

---领取奖励返回
function VipManager.getFetchReward(data)
    if data ~= nil then
        local rewards = {};
        for k,v in pairs(data) do
            local itemData = logicMgr.CostManager.getItemOrEquipDataByIdAndNum(v.itemID, v.itemNum);
            if itemData ~= nil then
                table.insert(rewards, itemData);
            end
        end
        if #rewards == 0 then
            this.closeItemGet();
        else
            showUI(PanelNames.UICommonItemGet, nil, nil, 3, rewards, this.closeItemGet)
        end
    end
end

function VipManager.closeItemGet()
    if this.getRewardCallBack then
        this.getRewardCallBack(this.getRewardtb, this.getRewardId);
    end
    this.getRewardId = nil;
    this.getRewardCallBack = nil;
    this.getRewardtb = nil;
end

---vip等级
function VipManager.getVipLevel()
    return this.vipLevel or 0;
end

function VipManager.setVipLevel(lv)
    this.vipLevel = lv or 0;
end

---vip总经验
function VipManager.getVipExp()
    return this.vipExp or 0;
end

---VIP奖励领取情况
function VipManager.getVipFetchList()
    return this.vipFetchList or {};
end

---获取VIP等级，当前进度与上限，下一个等级
function VipManager.GetVipInfo()
    local vipExp = VipManager.getVipExp();
    local nextLv = VipManager.GetVipTableDataByLv(VipManager.getVipLevel() + 1);
    local nowLv = VipManager.GetVipTableDataByLv(VipManager.getVipLevel());
    local rewardLv;
    local vipLv = tonumber(VipManager.getVipLevel());
    if vipLv ~= nil and vipLv > 0 then
        for i = 1,vipLv do
            local data, isReward = VipManager.GetVipTableDataByLv(i);
            if isReward == false and data.f_VipReward ~= "" then
                rewardLv = i;
                break;
            end
        end
    end
    if nextLv == nil then
        return nowLv.f_VipLevel, vipExp, nowLv.f_VipNeedExp, nil, rewardLv;
    end
    if nowLv == nil then
        nowLv = {};
        nowLv.f_VipLevel = 0;
        nowLv.f_VipNeedExp = 0;
    end
    return nowLv.f_VipLevel, vipExp, nextLv.f_VipNeedExp, nextLv.f_VipLevel, rewardLv;
end

---获取VIP是否有可领
function VipManager.GetVipIsHave()
    for k, v in pairs(vipTableData) do
        if v.f_VipLevel <= VipManager.getVipLevel() and v.f_VipReward ~= "" and not logicMgr.CostManager.isExistInListByValue(VipManager.getVipFetchList(), v.f_VipLevel) then
            return true;
        end
    end
    return false;
end

---获取VIP数据
function VipManager.GetVipTableDataByLv(id)
    for k, v in pairs(vipTableData) do
        if id == v.f_VipLevel then
            return v, logicMgr.CostManager.isExistInListByValue(VipManager.getVipFetchList(), id);
        end
    end
    return nil;
end

function VipManager.GetVipTableNum()
    local num = 0;
    if vipTableData ~= nil then
        for k, v in pairs(vipTableData) do
            num = num + 1;
        end
    end
    if this.vipLevel >= G.dataTable["t_global"][tostring(400)].f_int_value then
        return num;
    else
        return math.min(G.dataTable["t_global"][tostring(400)].f_int_value, num);
    end
end

---获取VIP的BUFF
function VipManager.getVipEcoBuff()
    local nowLv = VipManager.GetVipTableDataByLv(VipManager.getVipLevel());
    if nowLv == nil then
        return {};
    end
    return decodeJsonStrToTabel(nowLv.f_VipBuff);
end

--设置特权结束时间
function VipManager.setPrivilegeInfo(time, bugCount)
    local data = {};
    data.marketId = logicMgr.CostManager._PRIVILEGE;
    data.startTime = 0;
    data.endTime = -1;
    data.drawEndTime = -1;
    data.marketData = {};
    data.marketData.endTime = tonumber(tostring(time));
    data.marketData.bugCount = tonumber(tostring(bugCount));
    logicMgr.CostManager.initAllActive({data});
    --logicMgr.EcoBuffManager.updateEcoBuff(EnumConst.EcoBuffType.Nobility, this.getNobilityEcoBuff())
end

---获取特权BUFF
function VipManager.getNobilityEcoBuff()
    local severData, itemData, modelTableData;
    severData, itemData, modelTableData = logicMgr.CostManager.getCostData(logicMgr.CostManager._PRIVILEGE);
    if this.privilegeTime ~= nil then
        utimer.remove(this.privilegeTime);
    end
    if severData ~= nil and severData.overTime < 0 then
        return decodeJsonStrToTabel(modelTableData.f_NobleBuff);
    end
    if severData == nil or severData.overTime <= HelperFunc.GetServerTime() then
        return {};
    end
    this.privilegeTime = utimer.timeCall(function()
        logicMgr.EcoBuffManager.updateEcoBuff(EnumConst.EcoBuffType.Nobility, this.getNobilityEcoBuff());
        this.privilegeTime = nil;
    end, (severData.overTime - HelperFunc.GetServerTime()) * 1000, 1);
    return decodeJsonStrToTabel(modelTableData.f_NobleBuff);
end

return VipManager